package org.magicbox.qjunTPO.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.magicbox.qjunTPO.common.lang.Result;
import org.magicbox.qjunTPO.entity.Report;
import org.magicbox.qjunTPO.entity.Supuser;
import org.magicbox.qjunTPO.service.ReportService;
import org.magicbox.qjunTPO.service.SupuserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 欢迎添加科技实践队粉丝群：690849711
 * @since 2021-11-14
 */
@RestController
@RequestMapping("/report")
public class ReportController {
    @Autowired
    ReportService reportService;

    @Autowired
    SupuserService supuserService;

    @RequestMapping("/add")
    public Result report(@Validated @RequestBody Report report){
        LocalDateTime time = LocalDateTime.now();
        report.setReportCreate(time);
        reportService.save(report);
        return Result.success("举报成功");
    }

    @RequestMapping("/page/{suid}/{type}/{pageNumber}")
    public Result pageReport(@PathVariable("suid") Integer suid,@PathVariable("type") Integer type,@PathVariable("pageNumber") Integer pageNumber){
        Supuser supuser = supuserService.getById(suid);
        if(supuser==null) return Result.fail(402,"无权限");
        if(!supuser.getPoemOk().equals(1)) return Result.fail(403,"无权限");
        Page page = new Page(pageNumber,25);
        Map<String,Integer> eqMap = new HashMap<>();
        eqMap.put("type",type);
        IPage pageData = reportService.page(page,new QueryWrapper<Report>().allEq(eqMap).orderByDesc("report_create"));
        return Result.success(pageData);
    }

    /**
     * 管理员对举报进行处理调用该接口,需要传入
     * @param suid  管理员的id
     * @param type  举报的当前状态
     * @param reportid  举报的内容
     * @return 处理成功
     */
    @RequestMapping("/handle/{suid}/{type}/{reportid}")
    public Result handle(@PathVariable("suid") Integer suid,@PathVariable("type") Integer type,@PathVariable("reportid") Integer reportid){
        Supuser supuser = supuserService.getById(suid);
        if(supuser==null) return Result.fail(402,"无权限");
        if(!supuser.getPoemOk().equals(1)) return Result.fail(403,"无权限");
        List<Integer> list = new ArrayList();
        list.add(3);
        list.add(4);
        list.add(1);
        list.add(2);
        Report report= new Report();
        report.setReportid(reportid);
        report.setType(list.get(type-1));
        reportService.updateById(report);
        return Result.success("状态修改成功");
    }
}
